home *** CD-ROM | disk | FTP | other *** search
-
- I don't know if I saw it here or on a newsgroup, but I remember someone asking
- for a routine that maps the contents of a square bitmap into a trapezium (or
- trapezoid for the Americans out there).
-
- I came across an old message by Andy Church in the list's archives that
- explains it all.
-
- I hope it is appropriate to remail it here.
-
- (stuff after the .sig best viewed with a non-proportional font).
-
- --
- Branko Collin . |. .
- collin@xs4all.nl . . || ...
- http://www.xs4all.nl/~collin . ....||| .. ..
-
- :From: achurch@dragon.mbhs.edu (Andy Church)
- :To: amos-list@access.digex.net
- :Subject: Re: 3D Mapping
- :Date: Sun Aug 13 01:44:54 1995
-
- >Greetings one and all
- >
- >I want to map a picture of 320x320 pixels onto a polygon of horizontal
- >top and bottom edges, kinda like this:
- >
- > X1_________________X2 Y1
- > / \
- > / \
- > / \
- > / \
- > X3~~~~~~~~~~~~~~~~~~~~~~~~~X4 Y2
- >
- >If you know the four x,y coords, (X1,Y1 X2,Y1 X3,Y2 X4,Y2) how can
- >this be best achieved? I've tried for an hour or so, but I can't get
- >it to work :( and I'm doing A level maths! I bet I'm missing
- >something really obvious ;)
-
- Wow. I remember this stuff from graphics class. Real messy for the
- general case, at least as far as I got in it... I never actually finished
- implementing it. I'll try to explain as best I can... but it's going to
- use some advanced math. (Anyone else who has time and knows this, feel
- free to explain further, or correct me as necessary, or write up code. I'm
- way too busy for any of that. <sigh>)
-
- First off, you have the mapping direction backwards. Although it's
- counter-intuitive at first, you have to map *from* the polygon *to* the
- 320x320 bitmap. This is so that you draw each pixel on the polygon exactly
- once. If you go the other way, then you'll either draw the same pixel
- multiple times (if the polygon is smaller than the bitmap) or you'll have
- spaces between pixels (if the polygon is larger).
-
- Second, those have to be 3D coordinates, not 2D. You'll need a Z
- coordinate to go with each of the Y coordinates. I usually have Z increase
- going away from the viewer, with the screen being a plane of the form z=c (c
- is a constant).
-
- Anyway, this is the algorithm:
-
- - Pick one corner of the polygon to be your origin. (Let's use X1,Y1,Z1.)
-
- - Let the two edges containing the origin be vectors p and q. (I told you
- this was going to use advanced math...) Keep in mind that these will be 3D
- vectors. For this example, p is (X1,Y1,Z1)-(X2,Y1,Z1) and q is
- (X1,Y1,Z1)-(X3,Y2,Z2).
-
- - Let the two edges containing the matching corner of the bitmap be vectors p'
- and q'. Ugh, that was a messy sentence. Let me use a diagram:
-
- ________p________ __p'__
- /.................\ |......|
- q /. ....... \ |.. |
- /. ...... \ q'|. . |
- /. ...\ |. . |
- ~~~~~~~~~~~~~~~~~~~~~~~~~ |. . |
- |. .|
- ~~~~~~
- - For each point M:
-
- - Find values of a and b such that ap + bq = M. For the general case,
- this gets tricky; if you know that one of the vectors is horizontal, though,
- it becomes easier. Smething like this should work:
-
- A#=((M(x)-X1)*1.0)/(X2-X1)
- B#=((M(y)-Y1)*1.0)/(Y2-Y1)
-
- That may look like it won't work because X3-X4 seems longer than
- X1-X2. But in 3D coordinates, those are actually the same size.
- Those two lines are obviously not the most efficient way of doing
- things; they're merely pseudocode. (I use the M(x) notation to
- mean "the x component of M".)
-
- - Find the corresponding point M' on the bitmap; M' = ap' + bq'.
-
- - Get the color of that pixel on the bitmap and plot it in the
- polygon.
-
-
- It's late at night, so I might have missed something, gotten something
- completely wrong, etc... again, anyone else who knows this stuff, just
- correct me as necessary. <yawn>
-
- --Andy Church (achurch@binx.mbhs.edu)
- WWW: http://mmm.mbhs.edu/~achurch/
- AMOS Web Site: http://mmm.mbhs.edu/~achurch/amos/
-
-
-
-